package com.hoioy.findjob;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

//java基础数据类型
public class nio {
    
    public static void main(String[] args) throws IOException {
//        mapbyteBuf();
        int[] num = new int[]{2,4,5,9,1,4,0};
        int len = num.length;
        for(int i=0;i<len-1;i++){
            if(num[i]>num[i+1]){
                int t = num[i];
                num[i] = num[i+1];
                num[i+1]=t;
            }
        }
        System.out.println(num);
    }


    public static void mapbyteBuf() throws IOException {
//        FileInputStream inputStream = new FileInputStream("2.txt");
        RandomAccessFile inputStream = new RandomAccessFile("2.txt","rw");

        FileChannel fileChannel = inputStream.getChannel();
        MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        mappedByteBuffer.put(1, (byte) '1');
        mappedByteBuffer.put(3, (byte) 'a');
        inputStream.close();
    }


    public static void readonlyBuffer() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(20);
//        byteBuffer = byteBuffer.asReadOnlyBuffer();
        byteBuffer.putInt(2);

        byteBuffer.flip();
        System.out.println(byteBuffer.getInt());
    }

    public static void write() throws IOException {
        String ns = "你好啊，我要月薪5万";
        FileOutputStream outputStream = new FileOutputStream("2.txt");

        FileChannel fileChannel = outputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(ns.getBytes().length);

        byteBuffer.put(ns.getBytes());

        byteBuffer.flip();
        fileChannel.write(byteBuffer);

        outputStream.close();
    }

    public static void read() throws IOException {
        StringBuilder result = new StringBuilder();
        FileInputStream inputStream = new FileInputStream("2.txt");

        FileChannel fileChannel = inputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);

        while (true) {
//            byteBuffer.clear();
            int read = fileChannel.read(byteBuffer);
            byteBuffer.flip();
            result.append(new String(byteBuffer.array()));

//            System.out.println(byteBuffer);
            if(read==-1){
                break;
            }
        }

        System.out.println(result);
        inputStream.close();
    }

    public static void copy() {

    }
}
